package grouplife::Controller::Goods;
use Moose;
use namespace::autoclean;
use Catalyst::Request::Upload;
use String::Random;
use File::Basename;
use Image::Magick::Thumbnail::Fixed;
use Catalyst::Log;
use Data::Dumper;
use JSON;

use Data::Dumper;
use Catalyst qw/
Session
Session::Store::FastMmap
Session::State::Cookie
/;


BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

grouplife::Controller::Tuans - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut
sub add_goods : Local{
	my ($self, $c) = @_; 
	if (!$c->user_exists) {
		$c->log->debug('***Root::auto User not found, forwarding to /login');
		$c->response->redirect($c->uri_for('/login'));
		return 0;
	}   

	my $objectsource= $c->request->params->{objectsource};
	my $objecturl= $c->request->params->{objecturl};
	my $tuanid= $c->request->params->{tuanid};
	my $objectdesc= $c->request->params->{objectdesc};
	my $objectprice= $c->request->params->{objectprice};
	my $objectquantity= $c->request->params->{objectquantity};
	my $objectname= $c->request->params->{objectname};

	$c->log->info("source $objectsource");
	$c->log->info("url $objecturl");
	$c->log->info("tuanid $tuanid");
	$c->log->info("desc $objectdesc");
	$c->log->info("price $objectprice");
	$c->log->info("quantity $objectquantity");
	$c->log->info("name $objectname");

        my $t = new Image::Magick::Thumbnail::Fixed;
	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

	my $static = "/home/lijie/svn/grouplife/root/static/images";
	if(my $upload = $c->request->upload('objectimage')){
		my($filename, $directories, $suffix) = fileparse($upload->filename, qr/\.[^.]*/);
		$f_suffix = $suffix;
		my $rand = new String::Random;
		$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
		$year = 1900 + $year;
		$mon = 1 + $mon;
		my $dir = "/$year/$mon/$mday";

		$t_filename = $t_filename . $suffix;
		$tmp_target = "/tmp/$dir/$t_filename";

		system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
		system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
		system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
		$small_image = "$static/small/$dir/$t_filename";
		$big_image = "$static/big/$dir/$t_filename";
		$t_filename = "$dir/$t_filename";

		unless ( $upload->link_to($tmp_target) || $upload->copy_to($tmp_target) ) {
			die( "Failed to copy '$filename' to '$tmp_target': $!" );
		}

		$f_suffix =~ s/\.//;

#only create small thumnail for goods that are not from grouplife
		$t->thumbnail( input   => $tmp_target,
				output  => $small_image,
				width   => 150,
				height  => 150,
				density => '150x150',
				gravity => 'center',
				compose => 'over',
				quality => '90',
				bgcolor => 'white',
				format  => $f_suffix );
	} else {
		$t_filename = "good/default.jpg";
	}

	my $commented_item = $c->model('DB::CommentedItem')->create({
			comm_item_type_id => 3
			});

	my $goods = $c->model('DB::Good')->create({
			name => $objectname,
			price => $objectprice,
			quantity => $objectquantity,
			description => $objectdesc,
			source => $objectsource,
			url => $objecturl,
			comm_item_id => $commented_item->id,
			image => $t_filename
			});

	$goods->add_to_tuan_goods({tuan_id => $tuanid, relation => 'N'});
	$c->response->redirect($c->uri_for($c->controller('Tuans')->action_for('tuan_info'), [$tuanid]));
}


sub form_create_do : Local{
	my ($self, $c) = @_; 
	if (!$c->user_exists) {
		$c->log->debug('***Root::auto User not found, forwarding to /login');
		$c->response->redirect($c->uri_for('/login'));
		return 0;
	}   

	my $objectsource= $c->request->params->{objectsource};
	my $objecturl= $c->request->params->{objecturl};
	my $objectcategory = $c->request->params->{objectcategory};
	my $tuanid= $c->request->params->{tuanid};
	my $objectdesc= $c->request->params->{objectdesc};
	my $objectname= $c->request->params->{objectname};

	$c->log->info("source $objectsource");
	$c->log->info("url $objecturl");
	$c->log->info("category $objectcategory");
	$c->log->info("tuanid $tuanid");
	$c->log->info("desc $objectdesc");
	$c->log->info("name $objectname");

        my $t = new Image::Magick::Thumbnail::Fixed;
	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

	my $static = "/home/lijie/svn/grouplife/root/static/images";
	if(my $upload = $c->request->upload('objectimage')){
		my($filename, $directories, $suffix) = fileparse($upload->filename, qr/\.[^.]*/);
		$f_suffix = $suffix;
		my $rand = new String::Random;
		$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
		$year = 1900 + $year;
		$mon = 1 + $mon;
		my $dir = "/$year/$mon/$mday";

		$t_filename = $t_filename . $suffix;
		$tmp_target = "/tmp/$dir/$t_filename";

		system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
		system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
		system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
		$small_image = "$static/small/$dir/$t_filename";
		$big_image = "$static/big/$dir/$t_filename";
		$t_filename = "$dir/$t_filename";

		unless ( $upload->link_to($tmp_target) || $upload->copy_to($tmp_target) ) {
			die( "Failed to copy '$filename' to '$tmp_target': $!" );
		}

		$f_suffix =~ s/\.//;

		$t->thumbnail( input   => $tmp_target,
				output  => $small_image,
				width   => 150,
				height  => 150,
				density => '150x150',
				gravity => 'center',
				compose => 'over',
				quality => '90',
				bgcolor => 'white',
				format  => $f_suffix );

		$t->thumbnail( input   => $tmp_target,
				output  => $big_image,
				width   => 720,
				height  => 300,
				density => '720x300',
				gravity => 'center',
				compose => 'over',
				quality => '90',
				bgcolor => 'gray',
				format  => $f_suffix );
	} else {
		$t_filename = "good/default.jpg";
	}

	my $commented_item = $c->model('DB::CommentedItem')->create({
			comm_item_type_id => 3
			});

	my $goods = $c->model('DB::Good')->create({
			name => $objectname,
			description => $objectdesc,
			source => $objectsource,
			url => $objecturl,
			comm_item_id => $commented_item->id,
			image => $t_filename,
			});

	$goods->add_to_goods_category({category_id => $objectcategory});
	$goods->add_to_tuan_goods({tuan_id => $tuanid});

	$c->stash(good => $goods,
			login_user => $c->session->{login_user},
			nowrapper => 1,
			template => 'tuans/new_tuan.tt2');

}



=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->response->body('Matched grouplife::Controller::Goods in Goods.');
}

=head1 AUTHOR

lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

